﻿#include "GUIGeometryVerticalLineDialog.h"
#include "ui_GUIGeometryVerticalLineDialog.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGeometryData.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelOperLine.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeomTools.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelPoint.h"
#include "PickDataProvider/GUIPickInfo.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include "PickDataProvider/PickedData.h"
#include "PickDataProvider/PickedDataProvider.h"

#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsVirtualTopoMapper.h"

#include <QMessageBox>

namespace GUI {

    GUIGeometryVerticalLineDialog::GUIGeometryVerticalLineDialog(Core::FITKActionOperator* oper, QWidget* parent)
        : GUIGeometryBaseDialog(nullptr, oper, parent)
    {
        _ui = new Ui::GUIGeometryVerticalLineDialog();
        _ui->setupUi(this);
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        this->createGeometryObj(Interface::FITKGeoEnum::FITKGeometryComType::FGTSegment);//有限线段
        this->init();
    }


    GUIGeometryVerticalLineDialog::~GUIGeometryVerticalLineDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUIGeometryVerticalLineDialog::init()
    {
        setWindowTitle(tr("Vertical"));

        Acoustics::FITKAcousticsGeometryData* geometryData = FITKAPP->getGlobalData()->getGeometryData<Acoustics::FITKAcousticsGeometryData>();
        if (!geometryData) return;

        // 获取默认名称
        QString name = geometryData->checkName(tr("Vertical-1"));
        _ui->lineEdit_Name->setText(name);
    }

    QString GUIGeometryVerticalLineDialog::getNameText()
    {
        return _ui->lineEdit_Name->text();
    }

    void GUIGeometryVerticalLineDialog::pickFinishedOper()
    {
        if (_flag == 1)
        {
            //获取拾取数据
            GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
            if (provider == nullptr)return;
            disconnect(provider, SIGNAL(sig_pickedOver()), this, SLOT(slotPickOverEvent()));
            QList<GraphData::PickedData*> pickList = provider->getPickedList();
            QList<int> idList = {};
            QList<int> PickedIds;
            for (GraphData::PickedData* pickData : pickList) {
                if (pickData == nullptr)continue;
                idList.append(pickData->getPickedDataObjId());
                PickedIds = pickData->getPickedIds();
            }
            //获取拾取的数据
            if (idList.isEmpty()) return;

            //根据id获取点的xyz
            std::array<double, 3> xyz = this->getPointXYZ(PickedIds.at(0));
            _ui->Point_lineEditX->setText(QString::number(xyz.at(0)));
            _ui->Point_lineEditY->setText(QString::number(xyz.at(1)));
            _ui->Point_lineEditZ->setText(QString::number(xyz.at(2)));
        }
        else if (_flag == 2)
        {
            //获取拾取数据
            GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
            if (provider == nullptr)return;
            disconnect(provider, SIGNAL(sig_pickedOver()), this, SLOT(pickFinishedOper()));
            // 处理拾取数据
            QList<GraphData::PickedData*> pickList = provider->getPickedList();

            if (pickList.size() == 1) {
                GraphData::PickedData* pickData = pickList.at(0);
                if (pickData == nullptr) return;
                // 获取虚拓扑形状
                QList<int> virtualShapesID = pickData->getPickedIds();
                QHash<int, int> pickOther = pickData->getPickedOtherIDs();
                if (virtualShapesID.size() == 0 || !pickOther.contains(virtualShapesID.at(0))) return;
                _virtualShapes1.VirtualTopoId = virtualShapesID.at(0);
                _virtualShapes1.VirtualTopoIndex = pickOther.value(virtualShapesID.at(0));
                _virtualShapes1.CmdId = pickData->getPickedDataObjId();
                _virtualShapes1.Type = Interface::FITKGeoEnum::VTopoShapeType::VSEdge;
                _ui->label_Curve1->setText(_virtualShapes1.isNull() ? tr("Picked (0)") : tr("Picked (1)"));
            }
        }
        _flag = 0;
    }

    void GUIGeometryVerticalLineDialog::getDataFormWidget()
    {
        Interface::FITKAbsGeoModelSegment*lineP = dynamic_cast<Interface::FITKAbsGeoModelSegment*>(_obj);

        lineP->setDataObjectName(_ui->lineEdit_Name->text());//更新线名称

        //起点
        lineP->setStartPoint(
            _ui->Point_lineEditX->text().toDouble(),
            _ui->Point_lineEditY->text().toDouble(),
            _ui->Point_lineEditZ->text().toDouble()
        );
    }

    void GUIGeometryVerticalLineDialog::on_pushButtonPoint1_Pick_clicked()
    {
        //拾取对象 几何点
        //拾取方式 单独数据拾取方式。（点选+追加拾取）    
        _flag = 1;
        enablePickSig(GUI::GUIPickInfo::POBJGeometryVert, GUI::GUIPickInfo::PickMethod::PMSingle);
    }

    void GUIGeometryVerticalLineDialog::on_pushButtonPoint1_Pick_2_clicked()
    {
        //拾取对象 几何线
        //拾取方式 单独数据拾取方式。（点选+追加拾取）
        _flag = 2;
        enablePickSig(GUI::GUIPickInfo::POBJGeometryEdge, GUI::GUIPickInfo::PickMethod::PMSingle);
    }

    void GUIGeometryVerticalLineDialog::on_pushButton_OK_clicked()
    {
        if (_isCreate)
            if (!this->createObjData()) return;
            else
                if (!this->editObjData()) return;

        if (_oper && _obj) {
            _oper->setArgs("objID", _obj->getDataObjectID());
        }
        this->accept();
    }

    void GUIGeometryVerticalLineDialog::on_pushButton_Cancel_clicked()
    {
        this->close();
    }

    std::array<double, 3> GUIGeometryVerticalLineDialog::getPointXYZ(int id)
    {
        std::array<double, 3> xyz = { -1,-1,-1 };

        Interface::FITKInterfaceGeometryFactory*factory = Interface::FITKInterfaceGeometryFactory::getInstance();
        if (factory == nullptr)
        {
            return xyz;
        }
        Interface::FITKAbstractGeomToolsCreator*createor = factory->getGeomToolsCreator();
        Interface::FITKAbstractGeomPointTool *pointTool = createor->createPointTool();
        double PointArray[3];
        pointTool->getXYZ(id, PointArray);

        xyz.at(0) = PointArray[0];
        xyz.at(1) = PointArray[1];
        xyz.at(2) = PointArray[2];
        return xyz;
    }
}